<template>
  <swiper v-if="bannerList.length > 1" :options="swiperOption" ref="mySwiper">
    <!-- slides -->
    <swiper-slide v-for="(item, index) in bannerList" :key="index">
      <img :src="item" class="bannerImages" alt="" />
    </swiper-slide>
  </swiper>
</template>

<script>
import api from "@/api";
export default {
  name: "carrousel",
  data() {
    return {
      bannerList: [],
      swiperOption: {
        observer: true, //修改swiper自己或子元素时，自动初始化swiper
        observeParents: true, //修改swiper的父元素时，自动初始化swiper
        loop: true,
        autoplay: {
          delay: 3000,
          disableOnInteraction: false,
        },
      },
    };
  },
  computed: {
    swiper() {
      return this.$refs.mySwiper.swiper;
    },
  },
  mounted() {
    /**
     * 获取轮播图数据
     */
    api.getBanner().then((res) => {
      console.log(res.data);
      this.bannerList = res.data.images;
    });
  },
};
</script>
<style lang="less" scoped>
.bannerImages {
  width: 100%;
  height: 280px;
}
</style>